package org.kyw.basic.controller;

import org.kyw.basic.dto.LoginDto;
import org.kyw.basic.exception.BusinessException;
import org.kyw.basic.service.ILoginService;
import org.kyw.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 账号密码登录
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

    /**
     * 微信扫码登录
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> param){
        try {
            return loginService.wechat(param);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

    /**
     * 微信绑定接口
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map<String,Object> map = loginService.binder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

}
